
//
// 120825 - AH - Created.
//

package org.soekrat.server.serialization;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *   Represents ratbases.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class RatbasesSerializable implements Serializable, 
  Iterable<RatbaseSerializable>
{
  private static final long serialVersionUID=5209645985606013509L;
  
  private List<RatbaseSerializable> ratbases=
    new ArrayList<RatbaseSerializable>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public RatbasesSerializable(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param ratbases Represents the ratbases.
   */
  public RatbasesSerializable(RatbaseSerializable[] ratbases)
  {
    if (ratbases==null)
    {
      throw new NullPointerException("ratbases");
    }
    
    int n=ratbases.length;
    for (int i=0; i<n; i++) 
    {
      RatbaseSerializable curr=ratbases[i];
      if (curr==null)
      {
        throw new NullPointerException("ratbases["+i+"]");
      }
      
      this.ratbases.add(curr);
    }
  }
  
  /**
   * <p>
   *   Adds 
   *   {@link RatbaseSerializable}.
   * </p>
   * @param r {@code RatbaseSerializable} to add.
   */
  public void add(RatbaseSerializable r)
  {
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    this.ratbases.add(r);
  }
  
  /**
   * <p>
   *   Gets number of
   *   {@link RatbaseSerializable}.
   * </p>
   * @return Count.
   */
  public int size(){ return this.ratbases.size(); }
  
  /**
   * <p>
   *   Gets
   *   {@link RatbaseSerializable}.
   * </p>
   * @param idx Index of 
   *            {@code RatbaseSerializable}.
   * @return {@code RatbaseSerializable}.
   * @throws IndexOutOfBoundsException If {@code idx<0 || idx>=size()}.
   */
  public RatbaseSerializable getRatbase(int idx)
  { 
    return this.ratbases.get(idx);
  }

  // Specified in Iterable<RatBaseSerializable>. 
  public Iterator<RatbaseSerializable> iterator()
  { 
    return this.ratbases.iterator(); 
  }
  
  @Override
  public String toString()
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    for (RatbaseSerializable curr : this) pw.println(curr);
    
    pw.flush();
    return caw.toString();
  }
  
}
